<?php
namespace app\home\controller;
use think\captcha\Captcha;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use think\Cookie;
use think\jssdk;

class Index extends Controller{
    public function index()
    {
        $mem = new \Memcache();
        if(!$mem->connect('127.0.0.1',11211)){//连接缓存
            die();
        }
        if(!$mem->get("value")){//查看缓存是否存在   存在就读取  否则就新建
            $Navigation=Db::table("navigation")->select();
//        pre($Navigation);//打印导航
            $user=Db::table("carousel")->select();//查询carousel表
//        dump($user);//打印carousel表
//            $this->assign('user',$user);//把数据放到前台
            $array["user"]=$user;
            $arrays=null;
            $module=Db::table("module")->select();//查询模块
        //对应模块赋值
            for ($cc=0;$cc<count($module);$cc++){
                $module[$cc]["content"]=Db::table("content")->where("module_id=".$module[$cc]["id"])->find();
//            echo   $module[$cc]["content"]["content1"];
                $array1=Db::table("course")->where("id",$module[$cc]['content']['content1'])->find();
                $array2=Db::table("course")->where("id",$module[$cc]['content']['content2'])->find();
                $array3=Db::table("course")->where("id",$module[$cc]['content']['content3'])->find();
                unset( $module[$cc]["content"]["content1"],$module[$cc]["content"]["content2"],$module[$cc]["content"]["content3"],$module[$cc]["content"]["content4"],$module[$cc]["content"]["id"],$module[$cc]["content"]["module_id"]);
                $module[$cc]["content"]["array1"]=$array1;
                $module[$cc]["content"]["array2"]=$array2;
                $module[$cc]["content"]["array3"]=$array3;
                if(empty($module[$cc]["content"]["array1"])){
                    unset($module[$cc]["content"]["array1"]);
                }
                if(empty($module[$cc]["content"]["array2"])){
                    unset($module[$cc]["content"]["array2"]);
                }
                if(empty($module[$cc]["content"]["array3"])){
                    unset($module[$cc]["content"]["array3"]);
                }
            }
//        pre($arrays);//打印模块
            $array["navigation"]=$Navigation;
            $array["array1"]=$module;
//      pre($module[0]);//打印模块
//        echo    $array["head_img"];
//        echo     $array["head_img_url"];
//        var_dump( Cookie::has("username"));

//            $array["array2"]=$arrays;
            $mem->set("value",$array,0,300);  // 写缓存 成功返回true
        }
//        pre($mem->get("value"));

		if(Session::get('accessToken') != null){
            $ass = new jssdk('wx0e64e7f20a99049b',"086b2fc843046dfcacd054261ed3eb51");
            $as=$ass->getSignPackage();
            $this -> assign('sign',$as);
        }else{
            $as= null;
            $this -> assign('sign',$as);
        }

        $this->assign("username",user());
//        pre($mem->get("value"));
        return view("mousse",$mem->get("value"));
//        pre( $_COOKIE);
//        user();
//        pre($mem->get("value")["username"]);
    }


    public  function ajaxindex(){                                                         //这是主页ajax的请求 上拉加载
        $module=Db::table("module")->where("id","not in",$_POST["ids"])->find();
            $module["content"]=Db::table("content")->where("module_id=".$module["id"])->find();
//            echo   $module[$cc]["content"]["content1"];
            $array1=Db::table("course")->where("id",$module['content']['content1'])->find();
            $array2=Db::table("course")->where("id",$module['content']['content2'])->find();
            $array3=Db::table("course")->where("id",$module['content']['content3'])->find();
            unset( $module["content"]["content1"],$module["content"]["content2"],$module["content"]["content3"],$module["content"]["content4"],$module["content"]["id"],$module["content"]["module_id"]);
            $module["content"]["array1"]=$array1;
            $module["content"]["array2"]=$array2;
            $module["content"]["array3"]=$array3;
            if(empty($module["content"]["array1"])){
                unset($module["content"]["array1"]);
            }
            if(empty($module["content"]["array2"])){
                unset($module["content"]["array2"]);
            }
            if(empty($module["content"]["array3"])){
                unset($module["content"]["array3"]);
            }
//            pre($module);
        $id=Db::table("module")->count();
        if(($id-1)==$_POST["ids"]){//判断是否还有模块未加载
            $module["bug"]="true";
        }else{
            $module["bug"]="false";
        }

        echo json_encode($module);
    }



public function cancellation(){//注销方法
        Cookie::delete("username");
        Cookie::delete("head_img");
        Cookie::delete("user_id");
        Cookie::delete("user");
        Cookie::delete("PHPSESSID");
    $mem = new \Memcache();
    if(!$mem->connect('127.0.0.1',11211)){//连接缓存
        die();
    }
    $mem->delete("value");
       $this->redirect(url("Index/index")); //回调index
//    pre($_COOKIE);

}

    public function register_a()//返回注册页面
    {
        return view("register");
    }

    public function login(){//返回登录页面
		if(Session::get('accessToken') != null){
            $ass = new jssdk('wx0e64e7f20a99049b',"086b2fc843046dfcacd054261ed3eb51");
            $as=$ass->getSignPackage();
            $this -> assign('sign',$as);
        }else{
            $as= null;
            $this -> assign('sign',$as);
        }
        return view("login");
    }

    public function register(){
        //从前台获取值
        $data["username"]=$_POST["username"];
        $data["password"]=md5($_POST["password"]);
        $data["mobile"]=$_POST["number"];
        $data["home_phone"]=$_POST["home_phone"];
        $data["email"]=$_POST["email"];
        $code=$_POST["code"];
        $captcha=new Captcha();//创建验证码的类
        $code1=$captcha->check($code);//验证验证码
        $file = request()->file('image');
        if(!empty($data["username"]) && !empty($data["password"])&&!empty($data["mobile"])&&!empty($data["home_phone"])&&!empty($data["email"])){
            if($code1==true){//如果成功就执行注册操作
                $info = $file->move(ROOT_PATH . 'public' . DS . 'images');//创建一个文件夹
                $data['head_img']=$info->getSaveName();
                $register=Db::table('user')->insert($data);
                if($register==1){
                    $this->success("注册成功","Index/login");
                }else{
                    $this->success("注册失败","Index/register");
                }
            }else{
                $this->error("验证码错误");
            }
        }else{
            $this->error("输入不能为空");
        }
    }

    public function login_true(){ //登录操作
        $data=$_POST["mobile"];
        $data1=$_POST["password"];
//      直接匹配数据库；
        $login=Db::table("user")->where(["mobile"=>$_POST["mobile"],"password"=>md5($_POST["password"])])->find();
        if(!is_Null($login)){//登录成功后就设置cookie
            Cookie::set("username",$login["username"]);
            Cookie::set("user_id",$login["id"]);
            Cookie::set("head_img",$login["head_img"]);
            $this->redirect("Index/index");
//            pre($_COOKIE);
        }else{
            $this->error("登录失败",'Index/login');
        }
    }

    public function remote(){
//        判断是否为post提交
        if(Request::instance()->isPost()){
            if(Db::table('user')->where('mobile',$_POST['number'])->find()){
                echo json_encode('true');
            }else{
                echo json_encode('false');
            }
        }
    }

    public function remote_a(){
        //匹配在前台的email与数据库匹配；
        if(Db::table('user')->where('email',$_POST['email'])->find()){
            echo json_encode('true');
        }else{
            echo json_encode('false');
        }
    }

    public function modify(){
        return view("modify");
    }

    public function zhiye(){
        //通过id将数据从数据库拿出来传到zhiye里面;
        if(isset($_GET["picture_id"])){
            $course=Db::table("picture_course p ,course c")->where("p.picture_id=".$_GET["picture_id"]." and p.course_id=c.id ")->field("c.id,c.record_name,c.category,c.introduce,c.difficulty,c.charge")->select();
        }else{
            $course=Db::table("course")->limit(10)->select();
        }

        $picture=Db::table("picture")->select();
        return  view("zhiye",["course"=>$course,"user"=>$picture]);
    }
	public function lx(){
        //将留言板的值获取传到数据库里；
        if(!empty($_POST)){
            $content["content"]=$_POST["content"];
            $num=$_COOKIE["user_id"];
            $content["id"]=$num;
            $aa=Db::table("content_a")->insert($content);
            if($aa==1){
                $user=Db::view("content_a","cid,content")//通过id进行多表链接，将值直接传到页面上；
                    ->view("user","head_img","user.id=content_a.id")
                    ->select();
                $this->assign("user",$user);
                return view("ask");
            }else{
                echo "失败";
            }
        }else{
            $this->error("输入不能为空,请重新输入","Index/index");
        }
    }
    public function rea(){
        //获取从留言上传过来的id;
        $cc=$_GET["id"];
        $user= Db::view("hf","hid,h_content")//根据id查询hf里的数据;
            ->view("user","head_img","user.id=hf.user_id")
            ->where("cid",$cc)
            ->select();
        $this->assign("user",$user);//通过assign传值
        return view("ask_a",["cc"=>$cc]);
    }
    public function tur(){
        //将获取回复里的值；
        $content["cid"]=$_GET["cid"];
        $content["user_id"]=$_COOKIE["user_id"];
        $content["h_content"]=$_POST["content"];
        if(Db::table("hf")->insert($content)){//查询hf表里的值
            $user= Db::view("hf","hid,h_content")//多表查询；
                ->view("user","head_img","user.id=hf.user_id")
                ->where("cid",$content["cid"])
                ->select();
            $this->assign("user",$user);
            return view("ask_a",["cc"=>$content["cid"]]);
        }else{
            echo "0";
        }
    }
    public function ask(){
        //根据登录页面的id进行content_a的查询
       if(Cookie::has("user_id")){
           $user=Db::view("content_a","cid,content")
               ->view("user","head_img","user.id=content_a.id")
               ->select();
           $this->assign("user",$user);
           return view("ask");
       }else{
           $this->redirect(url("Index/login"));
       }
    }
	
	  public function phone(){//手机验证码
        if(Request::instance()->isPost()){
            $rand="";
            for($i=1;$i<=4;$i++){//随机生成手机的验证码;
                $rand.=rand(0,9);
            }
            Session::set('yzm',$rand);
            if(Db::table('user')->where('mobile',$_POST['phone'])->find()){//将验证码发送给用户;
                $this->redirect("http://106.ihuyi.com/webservice/sms.php?method=Submit&account=C88429033&password=e476ef653df3aec7e72519a49a7a0ece&mobile=".$_POST['phone']."&content=您的验证码是：" .$rand."。请不要把验证码泄露给其他人。");
            }else{
                echo "1";
            }
        }else{
            return view('phone');
        }
    }


    public function weixin(){ //绑定微信到账户
        if(Request::instance()->isPost()){
            if($_POST['yzm'] == Session::get('yzm')){
                $data['opendid'] = Session::get('openid');
                $data['WeChat'] = Session::get('nickname');
                $data['weixin_header'] = Session::get('headimgurl');
                $data['weixin_sex'] = Session::get('sex');
                $data['city'] = Session::get('city');
                if($login = Db::table('user')->where('mobile',$_POST['mobile'])->update($data)){
                    Cookie::set("username",$login["username"]);
                    Cookie::set("user_id",$login["id"]);
                    $this->redirect(url("Index/index"));
                }else{
                    $this->error("登录失败",'Index/login');
                }
            }else{
                $this->error("验证码输入有误，请重新获取");
            }
        }else{
            $this->error("数据传输有误");
        }
    }
	
	
	
	
	
}
